package com.monitor.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.monitor.entity.*;
import com.monitor.service.*;
import com.monitor.util.IDUtils;
import com.monitor.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 广告待审核表 前端控制器
 * </p>
 *
 * @author LX
 * @since 2018-09-25
 */
@RestController
@RequestMapping("/erpAdvertisementCheck")
@Api(description="广告表")
public class ErpAdvertisementCheckController {

    @Resource
    private ErpAdvertisementCheckService checkService;
    @Resource
    private ErpJjrService erpJjrService; //经纪人
    @Resource
    private ErpFbxxService erpFbxxService; //发布信息

    @Resource
    private ErpCustomerCarService erpCustomerCarService;

    @Resource
    private ErpCustomerService erpCustomerService;
    
    @Resource
    private ErpAdvertisementService erpAdvertisementService;
    
    @RequestMapping("/inertFbxx")
    @ApiOperation("添加发布信息")
    public @ResponseBody Result<Integer> inertFbxx(ErpFbxx erpFbxx) {
        Result<Integer>   result = new Result<>();
        try{
            ErpCustomerCar erpCustomerCar = erpCustomerCarService.selectOne(new EntityWrapper<ErpCustomerCar>().eq("car_plate", erpFbxx.getCarMessage()));
            Date currentTime = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateString = formatter.format(currentTime);
            erpFbxx.setFabuTime(dateString);
            if (null!=erpCustomerCar){
                erpFbxx.setMessage(erpCustomerCar.getCarPlate()+" "+erpCustomerCar.getCarBrand()+" "+erpCustomerCar.getCarModel()+" "+erpCustomerCar.getCarRims()+" "+erpFbxx.getMessage());
            }
            erpFbxx.setOrderNumber(IDUtils.genOrderId()+Math.random());
            result.setData(  erpFbxxService.insert(erpFbxx)?1:0);
            result.setMsg("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setData(0);
            result.setMsg("添加失败");
        }

        return result;
    }
    
    @RequestMapping("/getCheckNumber")
    @ApiOperation("获取广告审核单号")
    public Result<String> getCheckNumber(){
    	return checkService.getCheckNumber();
    }
    
 @RequestMapping("/inertJjr")
    @ApiOperation("添加竞技人相关")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "发布信息id", required = false, dataType = "int", paramType = "query"),
    })
            public @ResponseBody
            Result<Integer> inertJjr(ErpJjr erpJjr,Integer id) {
        Result<Integer>   result = new Result<>();
        try{

            ErpFbxx erpFbxx = erpFbxxService.selectById(id);
            Integer a=erpFbxx.getNumber()+1;
            erpFbxx.setNumber(a);
            erpFbxxService.updateById(erpFbxx);
            erpJjrService.insert(erpJjr);
        }catch (Exception e){
            e.printStackTrace();
            result.setData(0);
            result.setMsg("添加失败");
        }

        return result;
    }

    @RequestMapping("/selectFbxx")
    @ApiOperation("查询发布信息")
    @ApiImplicitParams({
    })
    public @ResponseBody
    Result<List<ErpFbxx>> selectFbxx(String cs) {
        Result<List<ErpFbxx>> result =new Result<>();
        try{

            List<ErpFbxx> message = erpFbxxService.selectList(new EntityWrapper<ErpFbxx>().like("message", cs));
            result.setData(message);
            result.setMsg("查询成功");
        }catch (Exception e) {

            e.printStackTrace();
            result.setMsg("查询失败");
        }
        return  result;
    }    @RequestMapping("/selectFbxxById")
    @ApiOperation("根据id查询发布信息")
    @ApiImplicitParams({
    })
    public @ResponseBody
    Result<List<ErpFbxx>> selectFbxxById(String name) {
        Result<List<ErpFbxx>>result =new Result<>();
        try{
            List<ErpFbxx> people = erpFbxxService.selectList(new EntityWrapper<ErpFbxx>().eq("people", name));
            result.setData(people);
            result.setMsg("查询成功");
        }catch (Exception e) {

            e.printStackTrace();
            result.setMsg("查询失败");
        }
        return  result;
    }
   @RequestMapping("/selectJjr")
    @ApiOperation("查询竞技人")
    @ApiImplicitParams({
    })
    public @ResponseBody
    Result<List<ErpJjr>> selectJjr(Integer id) {
        Result<List<ErpJjr>> result =new Result<>();
        try{
            List<ErpJjr> message = erpJjrService.selectList(new EntityWrapper<ErpJjr>().eq("fbxx_id",id));
            for (int i = 0; i < message.size(); i++) {
                ErpJjr erpJjr =  message.get(i);
                ErpCustomer erpCustomer1 = erpCustomerService.selectOne(new EntityWrapper<ErpCustomer>().eq("id", erpJjr.getCustId()));
                if (erpCustomer1.getStateGood()==0&&erpCustomer1.getChengGong()==0){
                    erpJjr.setStateGood("0%");
                }else
                    erpJjr.setStateGood(erpCustomer1.getStateGood()/erpCustomer1.getChengGong()*100+"%");
                erpJjr.setChengGong(erpCustomer1.getChengGong());
            }

            result.setData(message);
            result.setMsg("查询成功");
        }catch (Exception e) {

            e.printStackTrace();
            result.setMsg("查询失败");
        }
        return  result;
    }
  @RequestMapping("/updateFbxx")
    @ApiOperation("修改发布信息")
    @ApiImplicitParams({
    })
    public @ResponseBody
    Result<Integer> updateFbxx(ErpFbxx erpFbxx) {
        Result<Integer> result =new Result<>();
        try{
//                if (erpFbxx.getState().equals("完成")){
//                    暂时先不写了,完成的话缺少增加那个完成数量的功能
//                }
            result.setData(erpFbxxService.updateById(erpFbxx)?1:0);
            result.setMsg("添加成功");
        }catch (Exception e) {

            e.printStackTrace();
            result.setMsg("添加失败");
        }
        return  result;
    }  @RequestMapping("/updateJjr")
    @ApiOperation("修改竞技人")
    @ApiImplicitParams({
    })
    public @ResponseBody
    Result<Integer> updateJjr(ErpJjr erpJjr) {
        Result<Integer> result =new Result<>();
        try{
            result.setData(erpJjrService.updateById(erpJjr)?1:0);
            result.setMsg("添加成功");
        }catch (Exception e) {

            e.printStackTrace();
            result.setMsg("添加失败");
        }
        return  result;
    }

    @RequestMapping("/inertOrUpdatecheck")
    @ApiOperation("添加Or修广告表")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")*/
    })
    public @ResponseBody
    Result<Integer> inertOrUpdatecheck(ErpAdvertisementCheck insurance) {
        Result<Integer> result=new Result<Integer>();
        try{
        	if(insurance.getId()!=null&&insurance.getId()!=0) {
        		ErpAdvertisement adv=erpAdvertisementService.selectOne(new EntityWrapper<ErpAdvertisement>().eq("up_time_id", insurance.getId()));
        		if(adv!=null) {
	            	adv.setCheckNumber(insurance.getCheckNumber());
	            	adv.setContent(insurance.getContent());
	            	adv.setCheckPeople(insurance.getCheckPeople());
	            	adv.setContentColor(insurance.getContentColor());
	            	adv.setContentFamily(insurance.getContentFamily());
	            	adv.setContentSize(insurance.getContentSize());
	            	adv.setPlayRate(insurance.getPlayRate());
	            	adv.setCompany(insurance.getCompany());
	            	adv.setContract(insurance.getContract());
	            	adv.setTelphone(insurance.getTelphone());
	            	adv.setSubmitTime(insurance.getSubmitTime());
	            	String time=insurance.getEndTime();
	            	adv.setEndTime(time);
	            	adv.setCheckState("0");
	            	adv.setPrograms(insurance.getPrograms());
	            	adv.setShowTime(0);
	            	adv.setOrderNumber(0);
	                adv.setCheckTime("");
	                adv.setUpTimeId(insurance.getId());
	                erpAdvertisementService.updateById(adv);
        		}
        	}
        	insurance.setShowTime(0);
            if (insurance.getCheckTime()==null)
            insurance.setCheckTime("");
            result.setCount(1);
            result.setData(checkService.insertOrUpdate(insurance)?1:0);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/getcheckById")
    @ApiOperation("根据id查看广告信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "webId", value = "广告ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<ErpAdvertisementCheck> getInsuranceById(Integer webId) {
        Result<ErpAdvertisementCheck> result=new Result<ErpAdvertisementCheck>();
        try{
            result.setCount(1);
            result.setData(checkService.selectById(webId));
        }catch (Exception e){e.printStackTrace();}
        return result;                                              
    }

    @RequestMapping("/getcheckPage")
    @ApiOperation("根据客户ID查询广告分页")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")*/
    })
    public @ResponseBody
    Result<Page<ErpAdvertisementCheck>> getUserBycustIdPage(@RequestParam(value = "page", defaultValue = "1") int page,
                                                    @RequestParam(value="limit",defaultValue="10") int limit,@RequestParam(value="content",defaultValue="") String content) {
        Result<Page<ErpAdvertisementCheck>> result=new Result<Page<ErpAdvertisementCheck>>();
        try{
            Page<ErpAdvertisementCheck> page1=new Page<ErpAdvertisementCheck>(page,limit);
            Page<ErpAdvertisementCheck> carPage=checkService.selectPage(page1,new EntityWrapper<ErpAdvertisementCheck>().like("content",content));
            //Page<ErpCustomerCar> page=new Page<ErpCustomerCar>(1,2);
            result.setCount(carPage.getRecords().size());
            result.setData(carPage);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/removecheck")
    @ApiOperation("删除广告信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "webId", value = "广告ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Integer> removeInsurance(Integer webId) {
        Result<Integer>  result=new Result<Integer>();
        try{

            result.setCount(1);
            result.setData(checkService.deleteById(webId)?1:0);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/tiCheck")
    @ApiOperation("添加Or修广告表")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")*/
    })
    public @ResponseBody
    Result<Integer> tiCheck(ErpAdvertisementCheck insurance) {
        Result<Integer> result=new Result<Integer>();
        try{
        	ErpAdvertisement adv=new ErpAdvertisement();
        	adv.setCheckNumber(insurance.getCheckNumber());
        	adv.setContent(insurance.getContent());
        	adv.setCheckPeople(insurance.getCheckPeople());
        	adv.setContentColor(insurance.getContentColor());
        	adv.setContentFamily(insurance.getContentFamily());
        	adv.setContentSize(insurance.getContentSize());
        	adv.setPlayRate(insurance.getPlayRate());
        	adv.setCompany(insurance.getCompany());
        	adv.setContract(insurance.getContract());
        	adv.setTelphone(insurance.getTelphone());
        	adv.setSubmitTime(insurance.getSubmitTime());
        	String time=insurance.getEndTime();
        	adv.setEndTime(time);
        	adv.setCheckState("0");
        	adv.setPrograms(insurance.getPrograms());
        	adv.setShowTime(0);
        	adv.setOrderNumber(0);
        	insurance.setShowTime(0);
            insurance.setCheckTime("");
            result.setCount(1);
            result.setData(checkService.insertOrUpdate(insurance)?1:0);
            adv.setUpTimeId(insurance.getId());
            erpAdvertisementService.insert(adv);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

}

